//
//  UIViewController+SecondViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14-9-22.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//

#import "SecondViewController.h"
#import "MyOrdersTableViewCell.h"
#import "MyOrderDetailViewController.h"

@interface SecondViewController ()

@end

 @implementation SecondViewController

@synthesize dataList;
@synthesize passDelegate;

- (IBAction)showMenu
{
    [self.frostedViewController presentMenuViewController];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem=self.editButtonItem;
    
    NSDate *newDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    
    for(int i=0;i<10;i++){
        OrderData *data = [[OrderData alloc]init];
        
        [dateFormatter setDateFormat:@"MM-dd"];
        NSString *orderDate = [dateFormatter stringFromDate:newDate];
        
        data.date=orderDate;
        
        [dateFormatter setDateFormat:@"HH:mm"];
        data.time=[dateFormatter stringFromDate:newDate];
        
        if(i==2){
            data.state = @"已入场";
        }
        else if(i==3){
            data.state = @"已支付";
        }
        else if(i==4){
            data.state = @"已评价";
        }
        else{
            data.state = @"新订单";
        }
        
        NSString *t =[@"金茂大厦停车场" stringByAppendingFormat:@"%d", i];
        data.title = t;
        [tempArray addObject:data];
        
    }
    //分配当前视图控制器给表视图的委托和数据源
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //self.dataList = [tempArray copy];
    self.dataList = tempArray;
}

-(void)viewDidUnload{
    self.dataList = nil;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataList count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"myOrdersTableViewCellIdentifier";
    
    MyOrdersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(cell==nil){
        
        MyOrdersTableViewCell *dcell = [[MyOrdersTableViewCell alloc]init];
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyOrdersTableViewCell" owner:dcell options:nil] objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    NSUInteger row = [indexPath row];
    OrderData *rowData = [self.dataList objectAtIndex: row];
    cell.OrderInfo = rowData;
    cell.lbDate.text= rowData.date;
    cell.lbTime.text = rowData.time;
    cell.lbState.text = rowData.state;
    cell.lbTitle.text = rowData.title;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
   // UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 60)];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 34)];
    view.backgroundColor = [UIColor colorWithRed:167/255.0f green:167/255.0f blue:167/255.0f alpha:0.6f];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 0, 0)];
    label.text = @"订单：10";
    label.font = [UIFont systemFontOfSize:11];
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    [label sizeToFit];
    [view addSubview:label];
    return view;
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyOrdersTableViewCell *cell = ((MyOrdersTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]);
    
    UINavigationController *navigationController = (UINavigationController *)self.frostedViewController.contentViewController;
    
    MyOrderDetailViewController *myOrderDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"myOrderDetail"];
    
   
    self.passDelegate = myOrderDetailViewController;
    
    [navigationController pushViewController:myOrderDetailViewController animated:YES];
    
    //navigationController.pushviewControllers = @[@myOrderDetailViewController];
    
    [self.passDelegate passParameterToView:cell.OrderInfo.state];
    
    return nil;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSInteger row = [indexPath row];
        [self.dataList removeObjectAtIndex:row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        //[dataList removeObjectAtIndex: 0];
        //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
        //                      withRowAnimation:UITableViewRowAnimationFade];
        //[self.tableView reloadData];
    }
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [self.dataList count]) {
        return NO;
    } else {
        return YES;
    }
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
    if([view isKindOfClass:[SecondViewController class]]){
        return NO;
    }
    else{
        return YES;
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
#pragma mark -- UIViewController生命周期方法，用于响应视图编辑状态变化
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:YES];

}

#pragma mark --UITableViewDelegate 协议方法

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [self.dataList count]) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)dealloc {
   // [_lbOrdersTitle release];
   // [_tableView release];
   // [super dealloc];
}
@end
